//
//  MaskViewController.m
//  Pods
//
//  Created by Qianxia on 2016/12/22.
//
//

#import "MaskViewController.h"

@interface MaskViewController ()

@end

@implementation MaskViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    self.isTouchViewHidden = YES;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)viewDidAppear:(BOOL)animated {
    [super viewDidAppear:animated];
    
    if (animated) {
        // 执行动画
        [self showWithAnimations:nil completion:nil];
    }
}

#pragma mark - Custom

/**
 显示界面
 
 @param parentViewController 从哪个界面弹出， 传nil则默认为window.rootViewController
 */
- (void)showWithParentViewController:(UIViewController *)parentViewController {
    
    if (!parentViewController) {
        UIResponder<UIApplicationDelegate> *delegate = [UIApplication sharedApplication].delegate;
        parentViewController = delegate.window.rootViewController;
    }
    
    parentViewController.providesPresentationContextTransitionStyle = YES;
    parentViewController.definesPresentationContext = YES;
    self.modalPresentationStyle = UIModalPresentationOverCurrentContext;
    if (parentViewController.presentedViewController) {
        [parentViewController.presentedViewController dismissViewControllerAnimated:NO completion:nil];
    }
    [parentViewController presentViewController:self animated:NO completion:nil];
}


/**
 显示动画

 @param animations 显示动画
 @param completion 显示动画后执行
 */
- (void)showWithAnimations:(void (^)(void))animations completion:(void (^ __nullable)(BOOL finished))completion {
    
    // 设置背景色为透明
    self.view.backgroundColor = [UIColor colorWithWhite:0 alpha:0];
    [UIView animateWithDuration:0.35 animations:^{
        
        self.view.backgroundColor = [UIColor colorWithWhite:0 alpha:0.5];
        
        if (animations) {
            animations();
        }
    } completion:^(BOOL finished) {
        
        if (completion) {
            completion(finished);
        }
        
    }];
    
}


/**
 隐藏界面

 @param animations 隐藏动画
 @param completion 动画完成后执行
 */
- (void)hiddenWithAnimations:(void (^)(void))animations completion:(void (^ __nullable)(BOOL finished))completion  {
    
    [UIView animateWithDuration:0.35 animations:^{
        
        if (animations) {
            animations();
        }
        
        self.view.backgroundColor = [UIColor colorWithWhite:0 alpha:0];
        
    } completion:^(BOOL finished) {
        
        if (completion) {
            completion(finished);
        }
        
        // 主线程执行
        dispatch_async(dispatch_get_main_queue(), ^{
            if (self.parentViewController) {
                // 通知child，即将解除父子关系，从语义上也可以看出 child的parent即将为nil
                [self willMoveToParentViewController:nil];
                [self.view removeFromSuperview];
                // 通过removeFromParentViewController的调用真正的解除关系，removeFromParentViewController会自动调用 [content didMoveToParentViewController:nil]
                [self removeFromParentViewController];
                
            } else {
                [self dismissViewControllerAnimated:NO completion:nil];
            }
        });
        
    }];
}

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
    UITouch *touch = [touches allObjects].firstObject;
    
    // 点击界面隐藏
    if (touch.view == self.view && self.isTouchViewHidden) {
        [self hiddenWithAnimations:nil completion:nil];
    }
    
}

@end
